﻿using CcrFlows.Core;
using CcrFlows.Stages;
using Microsoft.Ccr.Core;

namespace CcrFlows.Internal.Stages
{
    class SeqStage<TIn, TOut> : IteratorStage<TIn, TOut>
    {
        internal SeqStage(SeqHandler<TIn, TOut> handler)
        {
            base.InitializeInputHandler(Arbiter.Receive(
                                        false,
                                        (Port<TIn>)base.inputPorts[0],
                                        i =>{
                                                handler(i, (Port<TOut>)base.outputPorts[0]);
                                                base.WaitForNextInput();
                                            }
                                        ));
        }


        internal SeqStage(SeqIteratorHandler<TIn, TOut> handler)
        {
            base.InitializeInputHandler(Arbiter.ReceiveWithIterator<TIn>(
                                        false,
                                        (Port<TIn>)base.inputPorts[0],
                                        i => base.ProcessIteratorHandler(i, handler, true)
                                        ));
        }        
    }
}
